##This file produces Figure 3. Gunshot Features with Averages from Loeffler (2014), "Detecting Gunshots Using Wearable Accelerometers"

require(lattice)
require(latticeExtra)
require(extrafont)
require(grid)
##font_import()
loadfonts(device = "postscript")

##Open training data
feature<-read.csv("C:/Users/cloef/Box Sync/Gunshot Detection/Data/training_long_201.csv")
feature2 <- na.omit(feature)
feature2$time <- (feature2$count-26)*0.3
features <- feature2[order(feature2$reltime),]

##Generate random subset of gunshot examples
randfeatures <- subset(features, (window %% 10) == 3) 

avg_features<-read.csv("C:/Users/cloef/Box Sync/Gunshot Detection/Data/avg_features_201.csv")
avgfeatures <- na.omit(avg_features)
avgfeatures$time <- (avgfeatures$count-26)*0.3


##Open postscript file
postscript("C:/Users/cloef/Box Sync/Gunshot Detection/Data/figure3.eps",
	paper="special",height=3.27,width=6.83,family="Arial",horizontal=FALSE)
par(omi=c(0.0278,0.0278,0.0278,0.0278))

##Generate x-axis random subset of gunshot examples
a<-xyplot(randfeatures$ax~randfeatures$time,type="l",col="grey",lty=1,lwd=0.5,group=randfeatures$window,ylim=range(c(-17,17)),xlim=range(c(-9.9,55)),
	xlab="ms",ylab="Gs",xlab.top="(a)",scales = list(col="black",tck = c(1, 0)),
	par.settings = list(axis.line=list(col = "transparent"), 
     	layout.heights=list(top.padding=-0.5, bottom.padding=-0.5,key.axis.padding=-0.75),
	layout.widths=list(left.padding=0.5,right.padding=-1.5,ylab.axis.padding = -0.5)),
	       axis = function(side, ...) {
           if (side == "left")
                grid.lines(x = c(0, 0), y = c(0, 1),
                           default.units = "npc")
           else if (side == "bottom")
                grid.lines(x = c(0, 1), y = c(0, 0),
                           default.units = "npc")
           axis.default(side = side, ...)
       })

##Generate x-axis averaged gunshot plot
b<-xyplot(avgfeatures$avg_ax~avgfeatures$time,type="l",col="black",lwd=0.75,ylim=range(c(-17,17)),xlim=range(c(-9.9,55)))

##Combine two x-axis plots together
ab <- a + as.layer(b)

#Generate y-axis random subset of gunshot examples
c<-xyplot(randfeatures$ay~randfeatures$time,type="l",col="grey",lty=1,lwd=0.5,group=randfeatures$window,ylim=range(c(-17,17)),xlim=range(c(-9.9,55)),
	xlab="ms",ylab="Gs",xlab.top="(b)",scales = list(col="black",tck = c(1, 0)),
	par.settings=list(axis.line=list(col = "transparent"),layout.heights=list(top.padding=-0.5, bottom.padding=-0.5,key.axis.padding=-0.75),
	layout.widths=list(left.padding=0.5,right.padding=-1.5,ylab.axis.padding = -0.5)),
      axis = function(side, ...) {
           if (side == "left")
                grid.lines(x = c(0, 0), y = c(0, 1),
                           default.units = "npc")
           else if (side == "bottom")
                grid.lines(x = c(0, 1), y = c(0, 0),
                           default.units = "npc")
           axis.default(side = side, ...)
       })

##Generate y-axis averaged gunshot plot
d<-xyplot(avgfeatures$avg_ay~avgfeatures$time,type="l",col="black",lwd=0.75,ylim=range(c(-17,17)),xlim=range(c(-9.9,55)))

##Combine two y-axis plots together
cd <- c + as.layer(d)

##Generate z-axis random subset of gunshot examples
e<-xyplot(randfeatures$az~randfeatures$time,type="l",col="grey",lty=1,lwd=0.5,group=randfeatures$window,ylim=range(c(-17,17)),xlim=range(c(-9.9,55)),
	xlab="ms",ylab="Gs",xlab.top="(c)",scales = list(col="black",tck = c(1, 0)),
	par.settings=list(axis.line=list(col = "transparent"),layout.heights=list(top.padding=-0.5, bottom.padding=-0.5,key.axis.padding=-0.75),
	layout.widths=list(left.padding=0.5,right.padding=-1.5,ylab.axis.padding = -0.5)),
      axis = function(side, ...) {
           if (side == "left")
                grid.lines(x = c(0, 0), y = c(0, 1),
                           default.units = "npc")
           else if (side == "bottom")
                grid.lines(x = c(0, 1), y = c(0, 0),
                           default.units = "npc")
           axis.default(side = side, ...)
       })

##Generate z-axis averaged gunshot plot
f<-xyplot(avgfeatures$avg_az~avgfeatures$time,type="l",col="black",lwd=0.75,ylim=range(c(-17,17)),xlim=range(c(-9.9,55)))

##Combine two z-axis plots together
ef <- e + as.layer(f)

print(ab, position = c(0, 0, 0.33, 1), more = TRUE)
print(cd, position = c(0.33, 0, 0.66, 1), more = TRUE)
print(ef, position = c(0.66,0,1,1))
dev.off()
